package com.cn.trip.DbDao;


import com.cn.trip.DaoUtil.DBHelper;
import com.cn.trip.bean.Account;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CardDao {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	
	public int addCard(String card,String password) {
		String sql="insert into account (cart_num,password) values (?,?)";
		conn= DBHelper.getConnection();
		
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, card);
			ps.setString(2, password);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	public Account selectCard(String card, String password) {
		String sql="select * from account where cart_num=? and password=?";
		Account account=null;
		conn=DBHelper.getConnection();
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, card);
			ps.setString(2, password);
			rs=ps.executeQuery();
			if(rs.next()) {
				account=new Account();
				account.setId(rs.getInt(1));
				account.setCart_num(rs.getString(2));
				account.setPassword(rs.getString(3));
				account.setBlance(rs.getFloat(4));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return account;
	}
	public int update(String card,float money) {
		String sql="update account set balance=balance-? where cart_num=?";
		conn=DBHelper.getConnection();
		
		try {
			ps=conn.prepareStatement(sql);
			ps.setFloat(1, money);
			ps.setString(2, card);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
